#include "mainpage.h"
#include "ui_mainpage.h"
#include "netmain.h"
#include <QDebug>
#include <QTimer>
#include <QPainter>
#include <QBrush>
#include <QPen>
#include <QPixmap>
#include <QJsonObject>

MainPage::MainPage(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainPage)
    , lightTimer(new QTimer)
{
    ui->setupUi(this);
    this->setFixedSize(300,300);
    showUp = true;
    showDown = true;
    showLeft = true;

    group0 = QColor(Qt::red);
    group1 = QColor(Qt::green);

    leftId = 0;
    upId = 1;
    downId = 2;
    leftRoad = 3;
    upRoad = 8;
    downRoad = 13;
    initColor();

    connect(lightTimer, &QTimer::timeout,
            this, &MainPage::lightTimerTimeoutSlot);
    lightTimer->start(500);
    connect(NetMain::getObject(),&NetMain:: modeChanged,
            this, &MainPage::modeChangedSlot);
}

MainPage::~MainPage()
{
    delete ui;
}

void MainPage::lightTimerTimeoutSlot()
{
    index++;
    if(index >= 112)
        index = 0;
    nightIndex++;
    if(nightIndex>=2)
        nightIndex = 0;
    sendStatus();
    update();
}

void MainPage::modeChangedSlot(int mode)
{
    this->mode = mode;
    this->update();
}

void MainPage::paintEvent(QPaintEvent *)
{
    QPainter painter;
    QPixmap pix(":/Image/background.jpg");
    QBrush brush;
    brush.setStyle(Qt::SolidPattern);
    QPen pen;
    painter.begin(&pix);

    if(showUp)
    {
        if(mode == 0){
            brush.setColor(group1ColorList[index]);
            pen.setColor(group1ColorList[index]);
        }
        else if (mode == 1) {
            brush.setColor(QColor(Qt::red));
            pen.setColor(QColor(Qt::red));
        }
        else if (mode == 2) {
            brush.setColor(nightColorList[nightIndex]);
            brush.setColor(nightColorList[nightIndex]);
        }
        else if (mode == 3) {
            brush.setColor(QColor(Qt::red));
            pen.setColor(QColor(Qt::red));
        }
        else if (mode == 4) {
            brush.setColor(QColor(Qt::green));
            pen.setColor(QColor(Qt::green));
        }
        painter.setBrush(brush);
        painter.setPen(pen);
        painter.drawRect(81, 48, 68, 11);
    }
    if(showDown)
    {
        if(mode == 0){
            brush.setColor(group1ColorList[index]);
            pen.setColor(group1ColorList[index]);
        }
        else if (mode == 1) {
            brush.setColor(QColor(Qt::red));
            pen.setColor(QColor(Qt::red));
        }
        else if (mode == 2) {
            brush.setColor(nightColorList[nightIndex]);
            brush.setColor(nightColorList[nightIndex]);
        }
        else if (mode == 3) {
            brush.setColor(QColor(Qt::red));
            pen.setColor(QColor(Qt::red));
        }
        else if (mode == 4) {
            brush.setColor(QColor(Qt::green));
            pen.setColor(QColor(Qt::green));
        }

        painter.setBrush(brush);
        painter.setPen(pen);
        painter.drawRect(81, 163, 68, 11);
    }
    if(showLeft)
    {
        if(mode == 0){
            brush.setColor(group0ColorList[index]);
            pen.setColor(group0ColorList[index]);
        }
        else if (mode == 1) {
            brush.setColor(QColor(Qt::red));
            pen.setColor(QColor(Qt::red));
        }
        else if (mode == 2) {
            brush.setColor(nightColorList[nightIndex]);
            brush.setColor(nightColorList[nightIndex]);
        }
        else if (mode == 3) {
            brush.setColor(QColor(Qt::green));
            pen.setColor(QColor(Qt::green));
        }
        else if (mode == 4) {
            brush.setColor(QColor(Qt::red));
            pen.setColor(QColor(Qt::red));
        }

        painter.setBrush(brush);
        painter.setPen(pen);
        painter.drawRect(53, 77, 11, 68);
    }
    if(showRight)
    {
        if(mode == 0){
            brush.setColor(group0ColorList[index]);
            pen.setColor(group0ColorList[index]);
        }
        else if (mode == 1) {
            brush.setColor(QColor(Qt::red));
            pen.setColor(QColor(Qt::red));
        }
        else if (mode == 2) {
            brush.setColor(nightColorList[nightIndex]);
            brush.setColor(nightColorList[nightIndex]);
        }
        else if (mode == 3) {
            brush.setColor(QColor(Qt::green));
            pen.setColor(QColor(Qt::green));
        }
        else if (mode == 4) {
            brush.setColor(QColor(Qt::red));
            pen.setColor(QColor(Qt::red));
        }
        painter.setBrush(brush);
        painter.setPen(pen);
        painter.drawRect(167, 77, 11, 68);
    }

    painter.end();
    painter.begin(this);
    painter.drawPixmap(0,0,width(), height(), pix);
    painter.end();
}

void MainPage::initColor()
{
    nightColorList[0] = QColor(Qt::yellow);
    nightColorList[1] = QColor(Qt::white);
    for(int i = 0; i < 40; i++)
        group0ColorList[i] = QColor(Qt::green);
    for(int i = 40; i < 50; i++)
        group0ColorList[i] = i % 2 == 0 ? QColor(Qt::green) : QColor(Qt::white);
    for(int i = 50; i < 56; i++)
        group0ColorList[i] = i % 2 == 0 ? QColor(Qt::yellow) : QColor(Qt::white);
    for(int i = 56; i < 112; i++)
        group0ColorList[i] = QColor(Qt::red);
    for(int i = 0; i < 56; i++)
        group1ColorList[i] = QColor(Qt::red);
    for(int i = 56; i < 96; i++)
        group1ColorList[i] = QColor(Qt::green);
    for(int i = 96; i < 106; i++)
        group1ColorList[i] = i % 2 == 0 ? QColor(Qt::green) : QColor(Qt::white);
    for(int i = 106; i < 112; i++)
        group1ColorList[i] = i % 2 == 0 ? QColor(Qt::yellow) : QColor(Qt::white);
}

void MainPage::sendStatus()
{
    QJsonObject sendObj;
    QJsonArray lamp;
    int roadId = getRoadIdUseLampId(0);
    if(roadId == -1)
        return;
    int color = getLampColorUseLampId(0);
    QJsonObject obj0;
    obj0.insert("lamp_id", 0);
    obj0.insert("color", color);
    obj0.insert("round_id", roadId);
    lamp.append(obj0);
    roadId = getRoadIdUseLampId(1);
    if(roadId == -1)
    {
        sendObj.insert("lamp", lamp);
        NetMain::getObject()->sendStatus(sendObj);
        return;
    }
    color = getLampColorUseLampId(1);
    QJsonObject obj1;
    obj1.insert("lamp_id", 1);
    obj1.insert("color", color);
    obj1.insert("round_id", roadId);
    lamp.append(obj1);

    roadId = getRoadIdUseLampId(2);
    if(roadId == -1)
    {
        sendObj.insert("lamp", lamp);
        NetMain::getObject()->sendStatus(sendObj);
        return;
    }
    color = getLampColorUseLampId(2);
    QJsonObject obj2;
    obj2.insert("lamp_id", 2);
    obj2.insert("color", color);
    obj2.insert("round_id", roadId);
    lamp.append(obj2);

    roadId = getRoadIdUseLampId(3);
    if(roadId == -1)
    {
        sendObj.insert("lamp", lamp);
        NetMain::getObject()->sendStatus(sendObj);
        return;
    }
    color = getLampColorUseLampId(3);
    QJsonObject obj3;
    obj3.insert("lamp_id", 3);
    obj3.insert("color", color);
    obj3.insert("round_id", roadId);
    lamp.append(obj3);
    sendObj.insert("lamp", lamp);
    NetMain::getObject()->sendStatus(sendObj);
}

int MainPage::getRoadIdUseLampId(int lampId)
{
    if(lampId == upId)
        return upRoad;
    if(lampId == downId)
        return downRoad;
    if(lampId == leftId)
        return leftRoad;
    if(lampId == rightId)
        return rightRoad;
    return -1;
}

int MainPage::getLampColorUseLampId(int lampId)
{
    if(mode == 1)
        return 2;
    if(mode == 2)
    {
        if(nightColorList[index] == QColor(Qt::yellow))
            return 1;
        if(nightColorList[index] == QColor(Qt::white))
            return 3;
    }
    if(lampId == upId || lampId == downId)
    {
        if(mode == 3)
            return 2;
        if(mode == 4)
            return 0;
        if(group1ColorList[index] == QColor(Qt::green))
            return 0;
        if(group1ColorList[index] == QColor(Qt::yellow))
            return 1;
        if(group1ColorList[index] == QColor(Qt::red))
            return 2;
        if(group1ColorList[index] == QColor(Qt::white))
            return 3;
    }
    if(lampId == leftId || lampId == rightId)
    {
        if(mode == 3)
            return 0;
        if(mode == 4)
            return 2;
        if(group0ColorList[index] == QColor(Qt::green))
            return 0;
        if(group0ColorList[index] == QColor(Qt::yellow))
            return 1;
        if(group0ColorList[index] == QColor(Qt::red))
            return 2;
        if(group0ColorList[index] == QColor(Qt::white))
            return 3;
    }
    return 2;
}

